# Makefile for Kernel
LD = ld
CC	:= gcc
ASM	:= ../comp/nasm
TARGET	= kernel.rsf
S = .
VPATH = $(S)

HEADER := $(S)
CFILES := $(notdir $(wildcard $(TARGET:%=$(S)/c/*.c))) $(notdir $(wildcard $(TARGET:%=$(S)/c/drivers/video/*.c))) $(notdir $(wildcard $(TARGET:%=$(S)/c/drivers/*.c))) $(notdir $(wildcard $(TARGET:%=$(S)/c/drivers/input/*.c)))

ASMFILES := $(notdir $(wildcard $(TARGET:%=$(S)/asm/*.asm)))
ACFILES	:= $(notdir $(wildcard $(TARGET:%=$(S)/c/*.S)))
INCLUDE	:= -I. -I$(S)/c -I$(S)/asm -I$(S)/c/include
#CFLAGS	:= -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector $(INCLUDE) -L../libs 
CFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 $(INCLUDE) 
AFLAGS	:= $(CFLAGS) 
#-D__ASSEMBLY__

# next define is for some header files that are to be used by both the
# kernel and user programs
#CFLAGS 	+= -DRYMOS_KERNEL
#CFLAGS	+= -fomit-frame-pointer
# CFLAGS	+= -O2
# -O2 doesn't work yet, some missing volatiles I guess

#COBJS   := kernel.o video.o io.o pic.o vga.o mm.o line.o windows.o \
#           box.o char.o label.o paging.o idt.o 

#ASMOBJS	:= k_entry.o registers.o 

ACOBJS	= $(addprefix $(S)/obj/, $(addsuffix .o, $(basename $(ACFILES))))
COBJS = $(addprefix $(S)/obj/, $(addsuffix .o, $(basename $(CFILES))))
ASMOBJS = $(addprefix $(S)/obj/, $(addsuffix .o, $(basename $(ASMFILES))))
OBJS	:= k_entry.o kernel.o $(COBJS) $(ASMOBJS) $(ACOBJS)


#CFILES  := $(COBJS:%.o=%.c)
#ASMFILES:= $(ASMOBJS:%.o=%.asm)
LIBDIR	= ../libs

$(TARGET) : $(OBJS)
	@echo
	@echo Creating $@
#	$(LD) -L$(LIBDIR) -lc -T kernel.ld -o $(TARGET) $(ASMOBJS) $(COBJS) 
	$(LD) -melf_i386 -T kernel.ld -o $(TARGET) k_entry.o kernel.o $(ASMOBJS) $(COBJS) $(ACOBJS) 
	@echo
	@echo "RYMOS kernel $@ compiled. "
	@echo "Type 'make disk' to copy the kernel to a floppy disk..."
	@echo Notice this disk must contain the DOS-FAT12 file system
	@echo and boot144.img as boot sector.



%.o : %.asm
	@echo Compiling $<
	$(ASM) -f elf $< -o $@

%.o : %.c
	@echo Compiling $<
	$(CC) $(CFLAGS) -c $< -o $@

$(S)/obj/%.o : $(S)/asm/%.asm
	@echo Compiling $<
	$(ASM) -f elf $< -o $@

$(S)/obj/%.o : $(S)/c/%.c
	@echo Compiling $<
	$(CC) $(CFLAGS) -c $< -o $@

$(S)/obj/%.o : $(S)/c/%.S
	@echo Compiling $<
	$(CC) $(AFLAGS) -c $< -o $@

$(S)/obj/%.o : $(S)/c/drivers/video/%.c
	@echo Compiling $<
	$(CC) $(CFLAGS) -c $< -o $@

$(S)/obj/%.o : $(S)/c/drivers/%.c
	@echo Compiling $<
	$(CC) $(CFLAGS) -c $< -o $@

$(S)/obj/%.o : $(S)/c/drivers/input/%.c
	@echo Compiling $<
	$(CC) $(CFLAGS) -c $< -o $@

#% : %.c
#	@echo Compiling $<
#	@$(CC) -O2 -o $@ $<



# clean up
.PHONY : clean
clean:
	#rm *.o
	rm *.rsf
	rm $(OBJS)
	@echo Clean maked.


# copy the compiled kernel to the disk (using mtools)
disk: kernel.rsf
	copy kernel.rsf a:\

test:
	echo $(OBJS)
